#include <opencv2/opencv.hpp>  
#include <opencv2/imgproc/types_c.h>
using namespace cv;  
using namespace std;  

#define WIDTH 640
#define HEIGHT 480

int main()  
{  
    VideoCapture capture;
    capture.open(0);
    if(!capture.isOpened())
    {
      std::cout<<"fail to open camera!"<<std::endl;
      return -1; 
    }
#if (CV_MAJOR_VERSION == 4)
    int width = capture.get(CAP_PROP_FRAME_WIDTH);
    int height = capture.get(CAP_PROP_FRAME_HEIGHT);
#else
    int width = capture.get(CAP_PROP_FRAME_WIDTH);
    int height = capture.get(CAP_PROP_FRAME_HEIGHT);
#endif
    std::cout<<"default width: "<<width<<std::endl;   
    std::cout<<"dufault height: "<<height<<std::endl;   
    width = WIDTH;
    height = HEIGHT;
#if (CV_MAJOR_VERSION ==4 )
    capture.set(CAP_PROP_FRAME_WIDTH, width);
    capture.set(CAP_PROP_FRAME_HEIGHT, height); 
#else
    capture.set(CV_CAP_PROP_FRAME_WIDTH, width);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT, height); 
#endif
    //write video
    cv::Size sWH = cv::Size(width, height); 
    string outputVideoPath = "./test.avi"; 
    VideoWriter outputVideo;
#if (CV_MAJOR_VERSION ==4 )
    outputVideo.open(outputVideoPath, VideoWriter::fourcc('M', 'P', '4', '2'), 25.0, sWH); 
#else
    outputVideo.open(outputVideoPath, CV_FOURCC('M', 'P', '4', '2'), 25.0, sWH); 
#endif
    int frameNum =200; 
    while (capture.isOpened())  
    {  
        Mat frame;  
        capture >> frame;
        //save frame to file 
        outputVideo << frame;
        frameNum--;
        if(frameNum == 0)
          break;
        
        imshow("camera", frame);  
        waitKey(30);    //延时30  
    }
    capture.release();
    outputVideo.release();
    return 0; 
} 
